Matlab tutorial Part 1
Intro to Matlab, step response

ME 461
Professor Tilbury


Note: These tutorials have been completely revised and updated. See Control Tutorials for Matlab for the latest version.
You should be able to run Matlab on the Macintoshes (it's on the key server, in the Software folder), PC's, or on any Unix machine. There are slightly different things you need to know for each format.
Macintosh
There is a built-in editor for m-files; you can also use any other editor you like (but be sure to save the files in text format).
Unix
Because of the extra security precautions that CAEN has installed on the lab workstations, you may need to type "xhost +localhost" before running Matlab. Using "xhost +" will also work but this is NOT RECOMMENDED as it will also allow anybody else to connect to your display. You will need to run an editor separately from Matlab. The best strategy is to make a directory for all your m-files for ME461, then cd to that directory before running Matlab and the editor.
PC's
I have never used Matlab on the PC's; you may be able to ask the TA's or CAEN for help if you have problems.

You can either type commands directly into matlab, or put all of the commands that you will need together in an m-file, and just run the file. If you put all of your m-files in the same directory that you run matlab from, then matlab will always find them.

We'll do problem 2.7 from the book (this was on homework 1). Create an m-file called "hw1.m" in your editor. Enter in the following lines:

m1 = 10;
m2 = 250;
Kw = 500000;
Ks = 10000;
b = 10000;
This defines all the constants. Since matlab can only deal with "real" numbers (i.e. not symbolic), it is important to enter in all the parameters before trying to use them to define other things. A semicolon at the end of each line will tell matlab not to print out the result of that command.
If you hate typing, try "cutting" the text out of this page (selecting it while holding the left mouse button down, or select and use command-C) and the "pasting" it into your editor (clicking the middle mouse button once, or using command-V).

Go over to the window where matlab is running, and type

hw1
Matlab will read in the contents of the file "hw1.m" (note that it assumes the file extension ".m"). You will not see any output (unless you forgot one of the semicolons or made a typing error). Check to see that the value of Kw is correct by giving matlab the command
Kw
It should return:
     Kw = 

          500000
Now enter the F,G,H,J matrices into the m-file:
F = [0 1 0 0
     -(Ks+Kw)/m1 -b/m1 Ks/m1 b/m1
     0 0 0 1
     Ks/m2 b/m2 -Ks/m2 -b/m2];
G = [0
     Kw/m1
     0
     0];
H = [1 0 0 0];
J = [0];
Note that there is a space between numbers and a new row of a matrix is signalled by the end of a line (return) with no punctuation. If you wanted matlab to keep reading and NOT start a new row, you need to end the line with three dots (...) For example, in the matlab window, type
list = [1 2 3 ...
     4 5 6]
contrast this with
list = [1 2 3 
     4 5 6]
(do not end the lines with semicolons; this will allow you to see the value that matlab assigns to the variable "list").

An equivalent way to enter the matrices is to put a semicolon at the end of each row, for example:
G = [0;  Kw/m1;  0;    0];
Also you might note that the transpose operation (') can be used to construct the G matrix:
G = [0  Kw/m1  0  0]' ;
Run your m-file again to read in the F,G,H,J matrices; type
hw1
again or use <ctrl>-p to go backwards through the commands you have already entered. (<ctrl>-n will bring you forwards if you go too far.) The up and down arrows may also work depending on your machine. Again, you should see no output unless you made a typo. It is a good idea to run an m-file frequently as you create it to eliminate pesky typos.

We now have enough information to compute the step response. As the last line in your m-file, enter:

step(F,G,H,J,1)
Remember that the "1" in the step command means use the first (in this case the only) input to compute the step response. Run your m-file again. A plot should come up on the screen which looks like It only shows the step response for the first 0.2 seconds. Let's try to see more of the response, say for 1 seconds. Go back to your m-file, and ABOVE the "step" line enter
t = 0:0.01:1;
This creates a vector whose first element is 0, last element is 1, and the elements in between are equally spaces at intervals of 0.01. Modify your step command to incorporate this time vecotor,
step(F,G,H,J,1,t)
and rerun your m-file. Verify that indeed one seconds worth of the response is shown.

Let's save the output of the step response into a vector so we can display it later. We do this by assigning the value of the step response to a variable, say y.

y = step(F,G,H,J,1,t);
No plot is generated when this command is entered. Even if you forget the semicolon, matlab will just print out the values of the vector y.

Let's see how to plot this vector using the "plot" command. First try

plot(t,y)
This is a very basic plot; the first variable is on the horizontal axis and the second variable is on the vertical axis. You can change the style of the line by using a third option to "plot", try
plot(t,y,':')
Note that the colon is enclosed in single quotes and separated from the other arguments by a comma. You should get a dotted line. You can make it red (if you have a color monitor) by using
plot(t,y,'r:')
Other options that you might want to try are:
solid   -      red       r
dashed  --     green     g
dotted  :      blue      b
dashdot -.     white     w                  
or try
help plot
for more information.
Now let's put some useful labels on the plot so someone who looks at it knows what it is. AFTER the plot command in your m-file, enter the lines:
xlabel('Time in seconds');
ylabel('Wheel position in meters');
title('Step response of a car wheel -- b = 10000 -- J. Doe, ME461, HW1');
Whew! The wheel position after the step input is done. We'll tackle the car position next. The system parameters (F,G) are the same, but the output is different (the H matrix changes). Use the same m-file, and add the lines:
Hcar = [0 0 1 0];
ycar = step(F,G,Hcar,1,t);
(if you were really conscientious at this point, you might edit the previous lines and rename y to ywheel and H to Hwheel, but we'll assume that they stay the same as before).

Instead of making a new plot for the car, let's just make one plot for both the car and the wheel. Say, the car is green solid and the wheel is red dashed. Don't forget to change the title of your plot, as well as the ylabel.

plot(t,y,'r--',t,ycar,'g-');

You might want to use the "text" command to make matlab place a piece of text at a certain point (x,y) of the plot. You can eyeball the plot and play around a bit to get it "just right". A good choice for this particular plot might be:

text(0.15,1.1,'car position')
text(0.1,1.3,'wheel position)
and Voila-- Finally, let's print it out. You need to know the name of the printer you want to use. This should be posted somewhere near the printer. Just type (within matlab)
print -P<printername>
or on a Mac, use the Print... command from the file menu.
If you would rather save the plot to print it later (say, you are logged in remotely and don't have a printer handy), then you would type
print plot.ps
to save the plot in a postscript format in a file called "plot.ps" (in your current working directory.) Sometime later, you could print it using the command "lpr -P<printername> plot.ps" Of course, if you are using a HP workstation to print, you would instead use the command "lpr -d<printername> plot.ps" (don't ask me why).
To try a different value of "b" for the shock absorber, just edit the appropriate line of the m-file and run it again. Don't forget to edit the title. Now you will see the advantage of entering all the variables sybolically at the beginning of the program, insteady of computing the values and entering them into the F and G matrices.


Matlab tutorial part 2

ME461 homepage


Last modified: 16 january 1996 / tilbury@umich.edu

Please send any comments, clarifications, suggestions, or corrections to: tilbury@umich.edu. Thanks! I hope you found this document helpful.